package src.origintrail;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.JPanel;
import src.origintrail.Player.Pace;
/**
 * Controls the end game gui
 * @author cassidy
 *
 */
public class ModuleEnd 
{
	private JLabel end;
	private Map map;
	private JLabel ask;
	private JButton yes;
	private JButton no;
	private ButtonListener listener;
        public static final JPanel endPanel = new JPanel();
        private static boolean isInitialized = false;

	/**
	 * constructor and initializes map.
	 */
	public ModuleEnd()
	{
		map = OriginTrail.plr.getMap();
	}
	/**
	 * method that draws the end module gui
	 */
	public void run()
	{
            if(isInitialized)
                endPanel.removeAll();
            isInitialized = true;
		OriginTrail.infoPanel.removeAll();
		OriginTrail.infoPanel.setLayout(new BorderLayout());
		endPanel.setLayout(new GridLayout(1,3));
                OriginTrail.uiPanel.setVisible(false);
		endPanel.setVisible(true);
		listener = new ButtonListener();
		ask = new JLabel("Would you like to play again?");
		yes = new JButton("Yes");
		no = new JButton("No");
		no.addActionListener(listener);
		yes.addActionListener(listener);
		end = new JLabel("You have reached " + map.getCurrentLocation()+ "! The End.");
		OriginTrail.infoPanel.add(end,BorderLayout.CENTER);
		endPanel.add(ask);
		endPanel.add(yes);
		endPanel.add(no);
                
                OriginTrail.otFrame.add(endPanel);
                OriginTrail.otFrame.pack();

	}

	/**
	 * class that listens for the user selection
	 * @author cassidy
	 *
	 */
	private class ButtonListener implements ActionListener
	{
            public boolean b = false;

		@Override
		public void actionPerformed(ActionEvent e) 
		{
			if(e.getSource() == yes)
			{
                            EvtSpawner.instance.spawnStartEvent();
                            OriginTrail.otFrame.remove(ModuleEnd.endPanel);
                            OriginTrail.infoPanel.removeAll();
                            try {
                                EvtSpawner.instance.getHandler().pop().activate();
                            } catch (Exception ex) {
                                Logger.getLogger(ModuleEnd.class.getName()).log(Level.SEVERE, null, ex);
                            }

                            }
			else if(e.getSource() == no)
			{
				OriginTrail.otFrame.dispose();
			}
				
			

		}
	}

}
